package category.dao.impl;

import category.dao.CategoryDao;
import category.entity.CategoryVO;
import generic.dao.impl.GenericDaoImpl;
import generic.page.Pages;

import java.util.List;

public class CategoryDaoImpl extends GenericDaoImpl<CategoryVO,Integer> implements CategoryDao {


    @Override
    public CategoryVO findCategoryVOById(Integer categoryId) {
        return (CategoryVO)this.findById(CategoryVO.class,categoryId);
    }

    @Override
    public CategoryVO existsOfCategory(CategoryVO categoryVO,Integer blogDataId) {
        Object[] params={categoryVO.getCategoryName(),blogDataId};
        List list=this.findObj("from CategoryVO c where c.categoryName=? and c.blogDataVO.blogDataId=?", params);
        if(list.size() == 0){
            return null;
        }else {
            return (CategoryVO)list.get(0);
        }
    }

    @Override
    public void deleteCategoryById(Integer categoryId) {
        this.delete(this.findCategoryVOById(categoryId));
    }

    @Override
    public CategoryVO updateCategory(CategoryVO categoryVO) {
        this.update(categoryVO);
        return categoryVO;
    }

    @Override
    public CategoryVO addCategoryVO(CategoryVO categoryVO) {
        this.save(categoryVO);
        return categoryVO;
    }

    @Override
    public Pages queryForPage(Pages pages,Integer blogDataId) {


        int pageSize=Pages.resetPageSize(pages.getPageSize());
        pages.setPageSize(pageSize);

        StringBuffer hql2= new StringBuffer();
        hql2.append("select count(*) from CategoryVO a where a.blogDataVO.blogDataId=").append(blogDataId);


        //当前页码
        final   int currentPage = Pages.countCurrentPage(pages.getCurrentPage());
        pages.setCurrentPage(currentPage);

        //总记录数
        Integer rowCount = Integer.parseInt(this.getSession().createQuery(hql2.toString()).uniqueResult().toString());
        pages.setRowCount(rowCount);

        //总页数
        final   int totalPage = Pages.countTotalPage(pageSize, rowCount); //总页数
        pages.setTotalPage(totalPage);

        //当前页开始的记录
        final  int offset = Pages.countOffset(pageSize, currentPage); //当前页开始记录

        //判断
        Pages.setPreOrNextBoolean(pages);

        //查询记录
        hql2.delete(0,15);
        List list = this.getSession().createQuery(hql2.toString())
                .setFirstResult(offset)
                .setMaxResults(pageSize).list();
        pages.setList(list);

        return pages;
    }

    @Override
    public Integer countArticleByCategory(Integer categoryId) {
        StringBuffer hql2= new StringBuffer();
        hql2.append("select count(*) from ArticleVO a where a.categoryVO.id=").append(categoryId);
        try {
            Integer num=Integer.parseInt(this.getSession().createQuery(hql2.toString()).uniqueResult().toString());
            return num;
        }catch (Exception e){
            return 0;
        }

    }

    @Override
    public List<CategoryVO> findAllCategoryByBlogData(Integer blogDataId) {
        Object[] params={blogDataId};
        List<CategoryVO> list=this.findObj("from CategoryVO c where c.blogDataVO.blogDataId=?", params);
        return list;
    }


}
